﻿using System;
using System.Windows.Forms;

namespace SPDevExplorer
{
    public partial class SiteExplorer
    {
        private void DeleteFolder(TreeNode selectedFolder)
        {
            try
            {
                FolderProperties fp = new FolderProperties(selectedFolder);
                DialogResult result =
                    MessageBox.Show(
                        String.Format("Are you sure you want to delete folder '{0}' with {1} items?", fp.Name,
                                      fp.ItemCount), "Confirm Delete", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    TreeNode parentFolderNode = selectedFolder.Parent;
                    string currentNodeText = null;
                    // Make next node the current node after deletion
                    if (selectedFolder.NextNode != null)
                    {
                        currentNodeText = selectedFolder.NextNode.Text;
                    }
                    else if (selectedFolder.PrevNode != null)
                    {
                        currentNodeText = selectedFolder.PrevNode.Text;
                    }

                    DevServices(selectedFolder).DeleteFolder(fp.ID);
                    RefreshFolder(parentFolderNode, currentNodeText);
                }
            }
            catch (Exception ex)
            {
                LogEvent(ex);
            }
        }

        /// <summary>
        /// Refresh the specified folder.
        /// </summary>
        /// <param name="folderNode">Folder node to refresh</param>
        private void RefreshFolder(TreeNode folderNode)
        {
            RefreshFolder(folderNode, null, null);
        }

        /// <summary>
        /// Refresh the specified folder.
        /// </summary>
        /// <param name="folderNode">Folder node to refresh</param>
        /// <param name="currentNodeText">The text in the node to make the current node (name of the file or folder). 
        /// If null the folder node is made the current ode, and the folder is shown in expanded state.</param>
        private void RefreshFolder(TreeNode folderNode, string currentNodeText)
        {
            RefreshFolder(folderNode, currentNodeText, null);
        }

        /// <summary>
        /// Refresh the specified folder.
        /// </summary>
        /// <param name="folderNode">Folder node to refresh</param>
        /// <param name="currentNodeText">The text in the node to make the current node (name of the file or folder). 
        /// If null the folder node is made the current ode, and the folder is shown in expanded state.</param>
        /// <param name="actionOnNode">Action to be executed on the node. Note that the node can be a folder node or a file node. If null no action is executed.</param>
        private void RefreshFolder(TreeNode folderNode, string currentNodeText, Action<TreeNode> actionOnNode)
        {
            FolderProperties fp = new FolderProperties(folderNode);
            // Make sure the node is properly refreshed
            ParentNodeAfterRefresh = folderNode;
            TextOfNodeToMakeCurrentAfterRefresh = currentNodeText;
            ActionOnNodeAfterRefresh = actionOnNode;
            folderNode.Collapse();
            folderNode.Nodes.Clear();
            LoadSubNodes(fp, folderNode);
        }
    }
}